#pragma once
#include <vector>
#include <map>

#define TOOL_BAR_START	AFX_IDW_CONTROLBAR_FIRST + 100
#define TOOL_BAR_END	TOOL_BAR_START + 100

#define CMD_START_ID TOOL_BAR_END + 1
#define CMD_END_ID	CMD_START_ID + 1000

class CBtnList : public CObList
{
};


struct HtCommand
{
	HtCommand()
	{
		spCmd = 0;
		nImageIndex = -1;
	}
	~HtCommand()
	{
		spCmd = 0;
	}
	long    nID;
	CString sDllName;
	CString sCaption;
	CString sCategory;
	CString sShowName;	
	int nVersion;
	int	nImageIndex;

	ICommandPtr		spCmd;

	bool GetCmdInfoByDllName(CString& sName,int nVer,long& nCmdID)
	{
		sDllName = sName;
		nVersion = nVer;
		spCmd.CreateInstance(sName);
		if(!spCmd)
			return false;

		nID = nCmdID++;
		sCaption = (LPCTSTR)spCmd->GetCaption();
		sCategory = (LPCTSTR)spCmd->GetCategory();

		return true;
	}
};

struct HtToolBar
{
	HtToolBar()
	{
		pToolBar = 0;
	}
	~HtToolBar()
	{
		delete pToolBar;
		pToolBar = 0;
		vtCmds.clear();
	}
public:
	CString sName;
	int		align;
	int		index;
	int		leftbar;
	CMFCToolBar* pToolBar;

	std::vector<HtCommand*>	vtCmds;

	int GetToolBarDock()
	{
		switch (align)
		{
		case 1:
			return CBRS_ALIGN_TOP;
		case 2:
			return CBRS_ALIGN_BOTTOM;
		case 3:
			return CBRS_ALIGN_LEFT;
		case 4:
			return CBRS_ALIGN_RIGHT;
		default:
			return CBRS_ALIGN_ANY;
		}		
	}

	void AddItem(HtCommand* pCmd)
	{
		vtCmds.push_back(pCmd);
	}

	void InitToolBar()
	{
		if(!pToolBar)
		{
			return;
		}

		CMFCToolBarImages* pImages = CMFCToolBar::GetUserImages();
		if(pImages == NULL)
			return;
	
		std::vector<HtCommand*>::iterator beg = vtCmds.begin();
		for (; beg != vtCmds.end(); ++beg)
		{
			HtCommand* pCmd = *beg;
			OLE_HANDLE hBitmap;
			HRESULT hr = pCmd->spCmd->get_Bitmap(&hBitmap);
			if(hBitmap)
				pCmd->nImageIndex = pImages->AddImage((HBITMAP)hBitmap);
		
			CMFCToolBarButton btn(pCmd->nID,-1,pCmd->sCaption);			
			btn.SetImage(pCmd->nImageIndex);
			btn.m_bText = FALSE;
			btn.m_bImage = TRUE;

			pToolBar->InsertButton(btn);
		}
	}

};
struct HtPanes 
{
	CString sCaption;
	int align;
	int dockto;
	CString sCoclass;
};

struct HtMenu
{
	HtMenu()
	{
		iPupup = 1;
		iMenuBar = 1;
		pHtCmd = 0;
	}
	~HtMenu()
	{
		std::vector<HtMenu*>::iterator beg = vtMenu.begin();
		for (; beg != vtMenu.end(); ++beg)
		{
			delete * beg;
		}
		vtMenu.clear();
		pHtCmd = 0;
	}
	int	iPupup;
	int iMenuBar;
	CString sCaption;
	HtCommand*	pHtCmd;
	std::vector<HtMenu*> vtMenu;
};
class CHtCommandManager
{
public:
	CHtCommandManager();
	~CHtCommandManager();

	long	m_nResourceID;

	void LoadCommandRes(CString& sAppPath);
	void GetListByTagName(CString sTagName);
	void GetPubInfo(MSXML2::IXMLDOMElementPtr& spRoot);
	void GetToolBar(MSXML2::IXMLDOMElementPtr& spRoot);
	void GetPanes(MSXML2::IXMLDOMElementPtr& spRoot);
	void GetCommand(MSXML2::IXMLDOMElementPtr& spRoot);
	void GetToolBarItem(MSXML2::IXMLDOMElementPtr& spRoot);
	void GetAllMenu(MSXML2::IXMLDOMElementPtr& spRoot);
	void GetMenuByElement(MSXML2::IXMLDOMElementPtr& spEle,HtMenu* pMenu = 0);

	template<typename T>
	void Clear(std::vector<T>& vtVector);

	void InitToolBar(CWnd* pParent);
	void InitMainMenu(CMFCMenuBar& wndMenuBar);
	void InitMainMenu(HMENU hMenu,std::vector<HtMenu*>& vtMenu);
	//void AddToolBarCommand(CMFCToolBar* pToolBar);
	HtToolBar*	FindToolBarByName(CString& sToolBarName);
	HtCommand* FindCommand(CString& sCategory,CString& sCaption);
	HtCommand* FindCommand(UINT id);

	std::vector<CString>		m_vtPubs;
	std::vector<HtToolBar*>		m_vtToolBar;
	std::vector<HtPanes>		m_vtPanes;
	std::vector<HtCommand*>		m_vtCommand;
	std::vector<HtMenu*>		m_vtMenu;	
};